package pl.praca.magisterska.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import pl.praca.magisterska.model.Order;
import pl.praca.magisterska.model.Person;
import pl.praca.magisterska.service.OrderService;
import pl.praca.magisterska.service.PersonService;

@Controller
@SessionAttributes
public class OrderController {

	private static Logger log = Logger.getLogger(OrderController.class);
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private Validator validator;

	public void setValidator(Validator validator) {
		this.validator = validator;
	}


	@RequestMapping(method=RequestMethod.POST, value="/setOrder")
	public @ResponseBody String setOrder(@RequestBody Order order) {
		log.info("setOrder: "+order);
		Integer result = orderService.insertOrder(order);
		
		if(order.getExecutionTimestamp().)
		
		
		if((result==null)||(result.equals(0))){
			return "NOK";
		}else{
			return "OK,"+result.toString();
		}	
	}
	
    
	@RequestMapping(method=RequestMethod.GET, value="/emp/test", 
			headers="Accept=application/json")
	public @ResponseBody Order[] getEmp() {
		Order e = new Order();
		e.setIdOrder(1);
		Order e2 = new Order();
		e2.setIdOrder(2);
		Order[] orders = {e,e2};
	    
	    return orders;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/emp")
	public @ResponseBody String addEmp(@RequestBody Order[] e) {
	System.out.println("test: "+ e);
	String result = "Zamówienie zostało przyjęte";
	return result;
	}
	
	@RequestMapping("/rest/matches/played")
	public ModelAndView getAllPlayedMatches() {
		 
	    List<Order> testList = getOrderList();
	    
	    		 
	    Order order = new Order();
	    order.setIdOrder(1);
	    Order[] orders = {order};
	    
	    ModelAndView mav = new ModelAndView();
	    mav.addObject("order", orders);
	    return mav;
	}
	
	@RequestMapping("/rest/employee/{id}")
	public ModelAndView getEmployee(@PathVariable String id) {
		Order test = new Order();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("matches", test);
		return mav;
	}
	
	@RequestMapping(value = "/rest/post", method=RequestMethod.POST)
	public String getPost(@RequestBody Order order) {
		String test = "test";
		
		return test;
	}
	
	private List<Order> getOrderList(){
		List<Order> orderList = new ArrayList<Order>();
		
		Order order1 = new Order();
	    order1.setIdOrder(1);
	    
	    orderList.add(order1);
//	    
//	    Order order2 = new Order();
//	    order2.setId("2");
//	    order2.setInput("order2");
//	    
//	    orderList.add(order2);
	    
	    return orderList;
	}
}
